<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-7-17
 * Time: 下午2:51
 */

namespace Actions;
use Data\Order_for_foods_DAO;
use Data\Order_form_DAO;
use Data\Food_DAO;
use Utils\SysUtils;

class RestaurantRetreatFood extends BaseAction
{
    public function doAction() {
        // 订单id
        $order_form_id = $this->data[ORDER_FORM_ID];
        // 退商品id
        $food_id = $this->data[FOOD_ID];
        // 订单记录
        $order_form_record = Order_form_DAO::getRecord($order_form_id);
        if (!$order_form_record) {
            return $this->SendErrMsg("错误，没有此订单信息");
        }
        // 商家id
        $restaurant_id = $order_form_record->getRESTAURANT_ID();
        // 是否已付款
        $order_form_pay_status = $order_form_record->getPAY_STATUS();
        if(!$order_form_pay_status) {
            return $this->SendErrMsg("订单未付款，不能退款");
        }
        // 商品记录
        $order_food_record = Order_for_foods_DAO::getRecordByFormIdFoodId($order_form_id,$food_id);
        if (!$order_food_record) {
            return $this->SendErrMsg("未点过此商品，不能退款");
        }
        // 商品是否已付款
        $food_pay_status = $order_food_record->getPAY_STATUS();
        if (!$food_pay_status) {
            return $this->SendErrMsg("此商品未付款，不能退款");
        }

        // 商品记录
        $food_record = Food_DAO::getRecord($food_id);
        if (!$food_record) {
            return $this->SendErrMsg("不存在此商品");
        }
        // 商品名称
        $food_name_retreat = $food_record->getFOOD_NAME();
        // 商品价格
        $food_price = $order_food_record->getFOOD_PRICE();
        // 商品数量
        $food_count = $order_food_record->getFOOD_COUNT();
        // 是否外带
        $is_take_out = $order_form_record->getIS_TAKE_OUT();
        // 退款数目
        $retreatMoney = $food_count*$food_price;
        $lunchbox_amounts = 0;
        $lunchbox_price = 0;
        if ($is_take_out==STATE_YES) {
            // 商品所需餐盒数量
            $lunchbox_amounts = $food_count*($food_record->getLUNCHBOX_AMOUNTS());
            // 餐盒价格
            $lunchbox_price = $food_count*($food_record->getLUNCHBOX_PRICE());
        }
        $retreatMoney += $lunchbox_price;

        // 订单中商品信息
        $order_for_food_list = Order_for_foods_DAO::getListByOrderFormId($order_form_id);
        foreach ($order_for_food_list as $key=>$order_for_food_record) {
            $food_id = $order_for_food_record[FOOD_ID];
            $food_name = Food_DAO::getName($food_id);
            $order_for_food_list[$key][FOOD_NAME] = $food_name;
        }
        // 订单全部信息

        $order_info = array(ORDER_FORM_INFO=>$order_form_record->getArray(),ORDER_FOR_FOOD_INFO=>$order_for_food_list,
            RETREAT_FOOD_INFO=>array(FOOD_NAME=>$food_name_retreat,FOOD_COUNT=>$food_count,LUNCHBOX_AMOUNTS=>$lunchbox_amounts,
                LUNCHBOX_PRICE=>$lunchbox_price,RETREAT_MONEY=>$retreatMoney));
        $sendData = array( ORDER_INFO => $order_info);
        // 推送

        SysUtils::notifyTofFrontEnd($restaurant_id,$sendData,EVENT_RESTAURANT_RETREAT_FOOD);

        $this->ResponseDataToCurrentClient();
    }
}